package com.jxc.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jxc.dao.BaseDaoI;
import com.jxc.model.po.Announcement;
import com.jxc.model.po.User;
import com.jxc.model.vo.DataGrid;
import com.jxc.model.vo.VAnnouncement;
import com.jxc.service.AnnouncementServiceI;
@Service("announcementService")
public class AnnouncementServiceImpl implements AnnouncementServiceI {

	@Autowired
	private BaseDaoI<Announcement> announcementDao;

	@Override
	public DataGrid datagrid(VAnnouncement vAnnouncement) {
		DataGrid dg = new DataGrid();
		dg.setRows(this.changeModel(this.find(vAnnouncement)));
		dg.setTotal(this.total(vAnnouncement));
		return dg;
	}

	@Override
	public void delete(String ids) {
		if (ids != null) {
			for (String id : ids.split(",")) {
				if (!id.trim().equals("0")) {
					Announcement announcement = announcementDao.get(
							Announcement.class, Long.parseLong(id.trim()));
					if (announcement != null) {
						announcement.setIsDeleted(true);
						announcementDao.update(announcement);
					}
				}
			}
		}

	}

	@Override
	public void save(VAnnouncement vAnnouncement) {
		Announcement announcement = new Announcement();
		BeanUtils.copyProperties(vAnnouncement, announcement);
		if (vAnnouncement.getUid() != null) {
			announcement.setUser(new User(vAnnouncement.getUid()));
		}
		announcement.setPublishDate(new Date());
		announcement.setIsDeleted(false);
		this.announcementDao.save(announcement);

	}

	@Override
	public void update(VAnnouncement vAnnouncement) {
		Announcement announcement = announcementDao.get(Announcement.class,
				vAnnouncement.getId());
		BeanUtils.copyProperties(vAnnouncement, announcement, new String[] {
				"id", "isDeleted" });
		if (vAnnouncement.getUid() != null) {
			announcement.setUser(new User(vAnnouncement.getUid()));
		}
		this.announcementDao.update(announcement);

	}

	private List<VAnnouncement> changeModel(List<Announcement> announcements) {
		List<VAnnouncement> vAnnouncements = new ArrayList<VAnnouncement>();
		if (announcements != null && announcements.size() > 0) {
			for (Announcement announcement : announcements) {
				VAnnouncement vAnnouncement = new VAnnouncement();
				BeanUtils.copyProperties(announcement, vAnnouncement);
				if (announcement.getUser() != null) {
					User user = announcement.getUser();
					vAnnouncement.setUid(user.getId());
					vAnnouncement.setUusername(user.getUsername());
				}
				vAnnouncements.add(vAnnouncement);
			}
		}
		return vAnnouncements;
	}

	private List<Announcement> find(VAnnouncement vAnnouncement) {
		String hql = "from Announcement t where 1=1 ";

		List<Object> values = new ArrayList<Object>();
		hql = addWhere(vAnnouncement, hql, values);

		if (vAnnouncement.getSort() != null && vAnnouncement.getOrder() != null) {
			hql += " order by " + vAnnouncement.getSort() + " "
					+ vAnnouncement.getOrder();
		}
		return announcementDao.find(hql, values, vAnnouncement.getPage(),
				vAnnouncement.getRows());
	}

	private Long total(VAnnouncement vAnnouncement) {
		String hql = "select count(*) from Announcement t where 1=1 ";
		List<Object> values = new ArrayList<Object>();
		hql = addWhere(vAnnouncement, hql, values);
		return announcementDao.count(hql, values);
	}

	private String addWhere(VAnnouncement vAnnouncement, String hql,
			List<Object> values) {
		if (vAnnouncement.getStartPublicDate() != null) {
			hql += " and t.publishDate>=?";
			values.add(vAnnouncement.getStartPublicDate());
		}
		if (vAnnouncement.getEndPublicDate() != null) {
			hql += " and t.publishDate<=?";
			values.add(vAnnouncement.getEndPublicDate());
		}
		hql += " and (t.endDate is null or t.endDate>?) and t.isDeleted = false ";
		values.add(new Date());
		return hql;
	}

	@Override
	public List<VAnnouncement> getAnnouncements(Integer size) {
		String hql = "from Announcement t where (t.endDate is null or t.endDate>?) and t.isDeleted = false order by t.publishDate desc";
		List<Object> values = new ArrayList<Object>();
		values.add(new Date());
		List<Announcement> list = announcementDao.find(hql, values, 1,size);
		return changeModel(list);
	}

}
